<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <filter-component :big-list="bigList" @filterChange="filterChange" />
        <el-table :data="pageList" border>
          <el-table-column width="80" label="姓名" prop="username" />
          <el-table-column width="80" label="工号" prop="workNumber" />
          <el-table-column width="120" label="手机号" prop="mobile" />
          <el-table-column width="120" label="部门" prop="departmentName" />
          <!-- 使用日期数组, 遍历生成三十一列 -->
          <el-table-column
            v-for="(day, index) in dayList"
            :key="day"
            :label="day"
            width="90"
          >
            <template #default="{row}">
              {{ row.attendanceRecord[index].adtStatu | formatStatu }}
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          layout="prev, pager, next"
          :total="filterList.length"
          :page-size="pageSetting.size"
          @current-change="currentChange"
        />
      </el-card>
    </div>
  </div>
</template>

<script>
import { getAttendancesMock } from '@/api/attendances'
import AttendanceEnum from '@/api/constant/attendance'
import FilterComponent from './components/filter.vue'
export default {
  components: { FilterComponent },
  filters: {
    formatStatu(id) {
      if (id === 0) {
        return ''
      }
      const obj = AttendanceEnum.holidayType.find(item => Number(item.id) === Number(id))
      return obj ? obj.value : '未知'
    }
  },
  data() {
    return {
      // 总数据
      bigList: [],
      // 过滤后的结果
      filterList: [],
      pageSetting: {
        page: 1,
        size: 10
      }
    }
  },
  computed: {
    dayList() {
      if (this.bigList.length === 0) {
        return []
      }
      return this.bigList[0].attendanceRecord.map(item => item.day.toString())
    },
    pageList() {
      // 之前有 bigList 一百条
      // 后面filterList 是过滤后的结果
      // 这里需要计算出每页开始和结束下标, 截取数组即可
      const endIndex = this.pageSetting.size * this.pageSetting.page
      const beginIndex = endIndex - this.pageSetting.size
      return this.filterList.slice(beginIndex, endIndex)
    }
  },
  async created() {
    this.bigList = await getAttendancesMock()
    this.filterList = [...this.bigList]
    console.log(this.bigList)
  },
  methods: {
    filterChange(newList) {
      this.filterList = newList
    },
    currentChange(newPage) {
      this.pageSetting.page = newPage
    }
  }
}
</script>

<style>

</style>
